﻿using System.Linq;
using System.Web.Mvc;
using Localization;
using SinBiota.Model.Business.PersistenceServices.Interfaces;
using SinBiota.Model.Entities;
using Sinbiota.Web.ActionFilters;

namespace Sinbiota.Web.Controllers
{
    public class DrainageBasinController : BaseController
    {
        private readonly IDrainageBasinPersistenceServices _drainageBasinPersistenceServices;

        public DrainageBasinController(IDrainageBasinPersistenceServices drainageBasinPersistenceServices, IUserPersistenceServices userPersistenceServices)
        {
            _drainageBasinPersistenceServices = drainageBasinPersistenceServices;
        }

        //
        // GET: /DrainageBasin/
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Index()
        {
            return View(_drainageBasinPersistenceServices.FindAll());
        }

        [HttpPost]
        [AdminPermission]
        public ActionResult Index(FormCollection collection)
        {
            return View(_drainageBasinPersistenceServices.FindByNameLike(collection[SearchBoxName]));
        }


        //
        // GET: /DrainageBasin/Details/5
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Details(int id)
        {
            var drainageBasin = _drainageBasinPersistenceServices.FindById(id);
            return drainageBasin == null ? View("NotFound") : View(drainageBasin);
        }

        //
        // GET: /DrainageBasin/Create
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /DrainageBasin/Create
        [HttpPost, ExportMessagesToTempData]
        [AdminPermission]
        public ActionResult Create(DrainageBasin drainageBasin)
        {
            var newDrainageBasin = new DrainageBasin();
            TryUpdateModel(newDrainageBasin);
            if (ModelState.IsValid)
            {
                _drainageBasinPersistenceServices.Save(newDrainageBasin);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(drainageBasin);
        }

        //
        // GET: /DrainageBasin/Edit/5
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Edit(int id)
        {
            var drainageBasin = _drainageBasinPersistenceServices.FindById(id);
            return drainageBasin == null ? View("NotFound") : View(drainageBasin);
        }

        //
        // POST: /DrainageBasin/Edit/5
        [HttpPost, ExportMessagesToTempData]
        [AdminPermission]
        public ActionResult Edit(int id, DrainageBasin drainageBasin)
        {
            var editDrainageBasin = _drainageBasinPersistenceServices.FindById(id);
            TryUpdateModel(editDrainageBasin);
            if (ModelState.IsValid)
            {
                _drainageBasinPersistenceServices.Update(editDrainageBasin);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(drainageBasin);
        }

        //
        // GET: /DrainageBasin/Delete/5
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Delete(int id)
        {

            var drainageBasin = _drainageBasinPersistenceServices.FindById(id);
            return drainageBasin == null ? View("NotFound") : View(drainageBasin);
        }

        //
        // POST: /DrainageBasin/Delete/5
        [HttpPost, ExportMessagesToTempData]
        [AdminPermission]
        public ActionResult Delete(int id, DrainageBasin drainageBasin)
        {
             var deleteDrainageBasin = _drainageBasinPersistenceServices.FindById(id);
            if (deleteDrainageBasin == null) return View("NotFound");
            _drainageBasinPersistenceServices.Delete(deleteDrainageBasin);
            SetInformationMessage(Messages.OperationCompletedSuccessfully);
            return RedirectToAction("Index");
        }
        // AJAX: 
        //Get all Basins: 
        public JsonResult GetAll()
        {
            return Json(_drainageBasinPersistenceServices.FindAll().Select(x => new { x.Id, x.Name }).OrderBy(i => i.Name));
        }
    }
}
